package com.example.chatgptapplication.controller.wx;


import com.example.chatgptapplication.dto.UserTagDTO;
import com.example.chatgptapplication.entity.Tag;
import com.example.chatgptapplication.entity.User;
import com.example.chatgptapplication.enums.ResponseStatus;
import com.example.chatgptapplication.relation.UserTag;
import com.example.chatgptapplication.service.TagService;
import com.example.chatgptapplication.service.UserService;
import com.example.chatgptapplication.service.UserTagService;
import com.example.chatgptapplication.utils.ResponseData;
import com.example.chatgptapplication.utils.UserHolder;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.attribute.standard.PrinterURI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wx")
public class WxController {


    @Resource
    private UserService userService;



    @Resource
    private TagService tagService;



    @Resource
    private UserTagService userTagService;


    @Autowired
    @Qualifier("caffeineCache")
    private Cache cache;




    @RequestMapping("/getUserStatus")
    public ResponseData getUserStatus(){
        User user = UserHolder.getUser();
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), user);
    }



    @RequestMapping("/userLogin")
    public ResponseData userLogin(@RequestBody User user){
        User u = userService.userLogin(user);
        if(u == null){
            return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), ResponseStatus.INTERNAL_SERVER_ERROR.getMessage(), "没有这个用户");
        }
        UserHolder.saveUser(u);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), u);
    }

    @PostMapping("/register")
    public ResponseData registerUser(@RequestBody User user){
        User u = userService.registerUser(user);
        if( u == null){
            return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), ResponseStatus.INTERNAL_SERVER_ERROR.getMessage(), "手机号:"+user.getAccount()+"已存在，请勿重复注册!");
        }
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), u);
    }


    @PostMapping("/getAllTags")
    public ResponseData getAllTags(){
        List<Tag> tags = tagService.list();
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), tags);
    }


    @PostMapping("/addUserTag")
    public ResponseData addUserTag(@RequestBody UserTagDTO userTagDTO){
        Integer userId = userTagDTO.getUserId();
        List<Tag> tags = userTagDTO.getTags();
        for(Tag item : tags){
            UserTag userTag = new UserTag();
            userTag.setUserId(userId);
            userTag.setTagId(item.getId());
            userTagService.save(userTag);
        }
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }


    @GetMapping("/getDailyChatLimit/{userId}")
    public ResponseData getDailyChatLimit(@PathVariable String userId){
        User user = userService.getUserInfoById(userId);
        if(user.getType().equals(0)){
            Integer count = (Integer) cache.asMap().get(userId);
            if(count == null){
                return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
            }else if(count >= 5){
                return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), "今天使用次数已达到上限" ,null);
            }
        }
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

    @PostMapping("/becomeVip")
    public ResponseData becomeVip(@RequestBody User user){
        user.setType(1);
        userService.updateById(user);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

}
